#region Using Statements

using System;
using System.Collections.Generic;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using ZombieHigh2.GameState;
using ZombieHigh2.Viewport;
using ZombieHigh2.Menus;
using ZombieHigh2.GameInput;

#endregion

namespace ZombieHigh2.Screens
{
    public enum MenuMode
    {
        MAIN_MENU,
        CONTROL_MENU
    }

    public class MenuScreen : IGameState
    {
        const int FINAL_CHOICE = 2;
        const int NEW_GAME = 0;
        const int CONTROLS = 1;
        const int QUIT = 2;

        Texture2D[] Choices;
        Texture2D ZombieHand;
        SpriteBatch spriteBatch;
        SpriteFont spriteFont;
        Texture2D controlLayoutTexture;
        int CurrentChoice;
        int nextToOpen;
        
        Boolean startNewGame = false;
        Boolean quiteGame = false;
        MenuMode menuMode;
        BasicEffect basicEffect;
        Matrix viewMatrix;
        Matrix projectionMatrix;

        AnimatedLocker[] lockers;
        //TextSwirl newGameSwirl;
        ContentManager content;

        public ContentManager Content { get { return content; } }
        public bool IsPaused { get { return false; } }

        public MenuScreen(ContentManager content)
        {
            this.content = content;
            CurrentChoice = 0;
            nextToOpen = 0;
            menuMode = MenuMode.MAIN_MENU;
            viewMatrix = Matrix.CreateLookAt(new Vector3(28, 50, 85), new Vector3(28, 12, 0), Vector3.Up);
            projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4,
                ViewportInfo.Instance.GetWidth / ViewportInfo.Instance.GetHeight, 1, 1000);

            lockers = new AnimatedLocker[4];
        }

        public void LoadGraphics(GraphicsDevice device)
        {
            Choices = new Texture2D[3];
            Choices[0] = content.Load<Texture2D>(@"Data\Images\newgame");
            Choices[1] = content.Load<Texture2D>(@"Data\Images\controls");
            Choices[2] = content.Load<Texture2D>(@"Data\Images\quit");

            ZombieHand = content.Load<Texture2D>(@"Data\Images\zombie hand");
            controlLayoutTexture = content.Load<Texture2D>(@"Data\Images\control_layout");

            spriteFont = content.Load<SpriteFont>(@"Data\Fonts\Arial");

            spriteBatch = new SpriteBatch(device);
            basicEffect = new BasicEffect(device, null);


            lockers[0] = new AnimatedLocker(Vector3.Zero);
            lockers[1] = new AnimatedLocker(new Vector3(18, 0, 0));
            lockers[2] = new AnimatedLocker(new Vector3(36, 0, 0));
            lockers[3] = new AnimatedLocker(new Vector3(54, 0, 0));

            lockers[0].LoadModels(content, @"Data\Models\model_new");
            lockers[1].LoadModels(content, @"Data\Models\model_load");
            lockers[2].LoadModels(content, @"Data\Models\model_load");
            lockers[3].LoadModels(content, @"Data\Models\model_exit");

            lockers[CurrentChoice].Open();

            
        }

        public void Load()
        {
        }

        public void UnloadGraphics()
        {
        }

        public void Update(GameTime gameTime)
        {
            float elapsedTime = (float)gameTime.ElapsedGameTime.TotalSeconds;
            InputManager.Instance.Update();

            if (menuMode == MenuMode.MAIN_MENU)
            {

                if (nextToOpen != CurrentChoice)
                {
                    if (lockers[CurrentChoice].IsOpen)
                    {
                        lockers[CurrentChoice].Close();
                    }
                    else if (lockers[CurrentChoice].IsClosed)
                    {
                        CurrentChoice = nextToOpen;
                        lockers[CurrentChoice].Open();
                    }
                }
                else if (lockers[CurrentChoice].IsOpen)
                {

                    //if (gpadState.DPad.Right == ButtonState.Pressed && !dpadDown)
                    if (InputManager.Instance.Pressed(Buttons.DPAD_RIGHT))
                    {
                        nextToOpen = CurrentChoice + 1;
                        if (nextToOpen > 3)
                        {
                            nextToOpen = 0;
                        }
                    }
                    else if (InputManager.Instance.Pressed(Buttons.DPAD_LEFT))
                    {
                        nextToOpen = CurrentChoice - 1;
                        if (nextToOpen < 0)
                        {
                            nextToOpen = 3;
                        }
                    }
                }

                if (InputManager.Instance.Pressed(Buttons.A) || InputManager.Instance.Pressed(Buttons.START))
                {
                    switch (CurrentChoice)
                    {
                        case NEW_GAME:
                            StartNewGame();
                            break;

                        case CONTROLS:
                            menuMode = MenuMode.CONTROL_MENU;
                            break;

                        case QUIT:
                            quiteGame = true;
                            break;
                    }
                }
            }
            else if (menuMode == MenuMode.CONTROL_MENU)
            {
                if (InputManager.Instance.Pressed(Buttons.A) || InputManager.Instance.Pressed(Buttons.START))
                {
                    menuMode = MenuMode.MAIN_MENU;
                }
            }

            //Update the locker animations
            foreach (AnimatedLocker locker in lockers)
            {
                locker.Update(elapsedTime);
            }
        }

        public void MoveChoiceDown()
        {
            CurrentChoice++;

            if (CurrentChoice > FINAL_CHOICE)
            {
                CurrentChoice = 0;
            }
        }

        public void MoveChoiceUp()
        {
            CurrentChoice--;

            if (CurrentChoice < 0)
            {
                CurrentChoice = FINAL_CHOICE;
            }
        }

        public void StartNewGame()
        {
            startNewGame = true;
        }

        public void Quit(GraphicsDevice device)
        {
            //            GameStateManager.Instance.GameHandle.Exit();
            CreditScreen creditScreen = new CreditScreen(content);
            creditScreen.LoadGraphics(device);
            GameStateManager.Instance.PushState(creditScreen);

        }

        public void Draw(GraphicsDevice device)
        {
            device.Clear(Color.Black);

            if (menuMode == MenuMode.MAIN_MENU)
            {
                //This is such a hack!
                if (startNewGame)
                {
                    GameScreen gameScreen = new GameScreen(content);
                    gameScreen.LoadGraphics(device);
                    GameStateManager.Instance.PushState(gameScreen);
                }
                else if (quiteGame)
                {
                    Quit(device);
                }
                else
                {
                    DrawMainMenu(device);
                }



            }
            else if (menuMode == MenuMode.CONTROL_MENU)
            {
                DrawControlLayout(device);
            }

        }

        public void DrawMainMenu(GraphicsDevice device)
        {
            Vector2 handPosition = Vector2.Zero;
            switch (CurrentChoice)
            {
                case NEW_GAME:
                    handPosition = new Vector2(80, 10);
                    break;

                case CONTROLS:
                    handPosition = new Vector2(80, 110);
                    break;

                case QUIT:
                    handPosition = new Vector2(80, 210);
                    break;
            }

            /* Rendering the locker */
            basicEffect.EnableDefaultLighting();
            basicEffect.Begin();
            foreach (AnimatedLocker locker in lockers)
            {
                locker.Draw(viewMatrix, projectionMatrix);
            }
            basicEffect.End();

            String copyright = "copyright 2007";
            int height = ViewportInfo.Instance.GetHeight;
            int center = ViewportInfo.Instance.GetWidth / 2;
            Vector2 fontOrigin = spriteFont.MeasureString(copyright) / 2;


            spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.SaveState);
            spriteBatch.DrawString(spriteFont, copyright, new Vector2(center, height - 60), Color.DarkGray,
                0f, fontOrigin, 1f, SpriteEffects.None, 1f);
            spriteBatch.End();
        }

        public void DrawControlLayout(GraphicsDevice device)
        {
            Rectangle viewArea = new Rectangle(0, 0, ViewportInfo.Instance.GetWidth, ViewportInfo.Instance.GetHeight);

            spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.SaveState);
            spriteBatch.Draw(controlLayoutTexture, viewArea, Color.White);
            spriteBatch.End();
        }

    }
}
